#write in 2019,6,2
#write by lizhuoying

import random

from sqlalchemy import create_engine,Column,Integer,String,func,ForeignKey

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker,relationship,backref


DB_URI='mysql+pymysql://root@127.0.0.1:3306/test?charset=utf8'

engine=create_engine(DB_URI)

Base=declarative_base(engine)#元类编程，将对象转化成元类

class Author(Base):
    __tablename__='author'
    id=Column(Integer,primary_key=True,autoincrement=True)
    name=Column(String(20))
    def __repr__(self):
        return '<Author (%s)>'%self.name

session=sessionmaker(engine)()

for i in range(20):
    author=Author(name="name%d"%i)
    session.add(author)
session.commit()

result=session.query(Author).limit(10)
# result=session.query(Author).offset(10)[4:6]
# result=session.query(Author).offset(10).slice(5,10)

print(result)


